

package Customer;
import ConnnectData.DataConnect;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import model.modelTable;

/**
 *
 * @author langtustupid
 */
public class Customer_Show extends javax.swing.JInternalFrame {

    Connection con;
    Statement sta;
    ResultSet result;
    public Customer_Show() {
        initComponents();
       con = DataConnect.connectDB();
    }
     

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Search = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbCustomer = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnAdd = new javax.swing.JMenu();
        mnDelete = new javax.swing.JMenu();
        mnShow = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Customer Management");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24));
        jLabel1.setText("Customer Management");

        jLabel2.setText("Customer Name :");

        jTextField_Search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField_SearchKeyReleased(evt);
            }
        });

        tbCustomer.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbCustomer.setRowHeight(20);
        tbCustomer.setSelectionBackground(new java.awt.Color(255, 153, 255));
        jScrollPane1.setViewportView(tbCustomer);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 898, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addGap(47, 47, 47)
                        .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel2Layout.createSequentialGroup()
                        .addGap(270, 270, 270)
                        .addComponent(jLabel1)))
                .addContainerGap(404, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(39, 39, 39)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 68, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));

        mnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/add.gif"))); // NOI18N
        mnAdd.setText(" Add Customer");
        mnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnAddMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnAdd);

        mnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/exit.png"))); // NOI18N
        mnDelete.setText("Delete Customer ");
        mnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDeleteMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDelete);

        mnShow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/star.gif"))); // NOI18N
        mnShow.setText("Show Customer");
        mnShow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnShowMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnShow);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDeleteMouseClicked
       if (tbCustomer.getSelectedRow() != -1) {
            int remove = JOptionPane.showConfirmDialog(this, "Are you sure want delete this Customer !");
            if (remove == 0) {
                int index = Integer.parseInt(tbCustomer.getValueAt(tbCustomer.getSelectedRow(), 0).toString());
                try {
                    String sql_del = "Update Customer set Cuatomer_status = 0 where Customer_Id=" + index;
                    sta.executeUpdate(sql_del);
                    result = sta.executeQuery("Select *from Customer");
                    tbCustomer.setModel(new modelTable(result));
                } catch (Exception e) {
                    e.printStackTrace();
                }

                
                JOptionPane.showMessageDialog(this, "Delete Customer success ! ");
            }

        } else {
            JOptionPane.showMessageDialog(null, "You should choose the Customer want to delete !");
        }
    }//GEN-LAST:event_mnDeleteMouseClicked

    private void mnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnAddMouseClicked
        new Add_Customer().setVisible(true);
    }//GEN-LAST:event_mnAddMouseClicked

    private void mnShowMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnShowMouseClicked
      try {
        sta = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        String sql = "Select Customer_Id,Customer_Name,Customer_Sex,Customer_Age,Customer_Phone,Customer_Address,Customer_Email from Customer where Cuatomer_status = 1";
        result = sta.executeQuery(sql);
        tbCustomer.setModel(new modelTable(result));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    }//GEN-LAST:event_mnShowMouseClicked

    private void jTextField_SearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_SearchKeyReleased
       try {
            result = sta.executeQuery("Select *from Customer where Customer_Name like '%" + jTextField_Search.getText().trim() + "%'");
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }//GEN-LAST:event_jTextField_SearchKeyReleased

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
}//GEN-LAST:event_mnCloseMouseClicked

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Customer_Show().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField_Search;
    private javax.swing.JMenu mnAdd;
    private javax.swing.JMenu mnClose;
    private javax.swing.JMenu mnDelete;
    private javax.swing.JMenu mnShow;
    private javax.swing.JTable tbCustomer;
    // End of variables declaration//GEN-END:variables

}
